package com.joymart.user.service;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.joymart.common.error.handling.JoymartBusinessException;
import com.joymart.common.error.handling.JoymartInternalException;
import com.joymart.common.system.IdGenerator;
import com.joymart.common.utils.CollectionUtils;
import com.joymart.common.utils.DatetimeUtils;
import com.joymart.user.model.BusinessRole;
import com.joymart.user.model.User;
import com.joymart.user.model.UserHealthReport;
import com.joymart.user.model.UserPassword;
import com.joymart.user.repo.BusinessRoleRepository;
import com.joymart.user.repo.UserPasswordRepository;
import com.joymart.user.repo.UserRepository;
import com.joymart.wechat.service.WechatOpenPlatformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jdbc.core.JdbcAggregateTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.nio.ByteBuffer;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.zip.CRC32;

@Service
public class UserHealthReportService {

    @Autowired
    private JdbcAggregateTemplate jdbcAggregateTemplate;

    public UserHealthReport findByUserId(String userId) {
       return jdbcAggregateTemplate.findById(userId, UserHealthReport.class);
    }

    public void upsertUserHealthReport(String userId, String url) {
        UserHealthReport report = this.findByUserId(userId);
        if(report == null){
            UserHealthReport newReport = new UserHealthReport();
            newReport.setUserId(userId);
            newReport.setUrl(url);
            newReport.setCreatedAt(DatetimeUtils.now());
            jdbcAggregateTemplate.insert(newReport);
        }else{
            report.setUrl(url);
            report.setCreatedAt(DatetimeUtils.now());
            jdbcAggregateTemplate.update(report);
        }
    }

}
